package juanca.mole;

import java.sql.Time;
import java.text.SimpleDateFormat;

import ClasesExtras.ManejadorCampos;
import ClasesExtras.handlerAlertMessage;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TimePicker;
import android.widget.Toast;

public class CheqFQ_Traza extends Activity {

	private int hMinuto;
	private int hHora;
	private boolean color;
	private boolean olor;
	private boolean sabor;
	private boolean textura;
	private String obsColor;
	private String obsOlor;
	private String obsSabor;
	private String obsTextura;
	private String medPh;
	private String consistencia;
	private String acidez;
	private String brix;
	public Time tiempoMezcla;

	public static final int TIME_DIALOG_ID = 1;
	public static final String STRING_EMPTY = "";

	private EditText etMedPhTraza;
	private EditText etConsisTraza;
	private EditText etAcidezTraza;
	private EditText etBrixtraza;

	private EditText etObsPre1Traza;
	private EditText etObsPre2Traza;
	private EditText etObsPre3Traza;
	private EditText etObsPre4Traza;
	private RadioButton rbSiResPre1Traza;
	private RadioButton rbNoResPre1Traza;
	private RadioButton rbSiResPre2Traza;
	private RadioButton rbNoResPre2Traza;
	private RadioButton rbSiResPre3Traza;
	private RadioButton rbNoResPre3Traza;
	private RadioButton rbSiResPre4Traza;
	private RadioButton rbNoResPre4Traza;
	private ManejadorCampos manejador;
	private handlerAlertMessage manejadorMensajes;
	private Activity activity;

	/**
	 * @return the brix
	 */
	public Time getTiempoMezcla() {
		return tiempoMezcla;
	}

	/**
	 * @param brix
	 *            the brix to set
	 */
	public void setTiempoMezcla(Time tiempoMezcla) {
		this.tiempoMezcla = tiempoMezcla;
	}

	/**
	 * @return the brix
	 */
	public String getBrix() {
		return brix;
	}

	/**
	 * @param brix
	 *            the brix to set
	 */
	public void setBrix(String brix) {
		this.brix = brix;
	}

	/**
	 * @return the acidez
	 */
	public String getAcidez() {
		return acidez;
	}

	/**
	 * @param acidez
	 *            the acidez to set
	 */
	public void setAcidez(String acidez) {
		this.acidez = acidez;
	}

	/**
	 * @return the consistencia
	 */
	public String getConsistencia() {
		return consistencia;
	}

	/**
	 * @param consistencia
	 *            the consistencia to set
	 */
	public void setConsistencia(String consistencia) {
		this.consistencia = consistencia;
	}

	/**
	 * @return the medPh
	 */
	public String getMedPh() {
		return medPh;
	}

	/**
	 * @param medPh
	 *            the medPh to set
	 */
	public void setMedPh(String medPh) {
		this.medPh = medPh;
	}

	/**
	 * @return the obsTextura
	 */
	public String getObsTextura() {
		return obsTextura;
	}

	/**
	 * @param obsTextura
	 *            the obsTextura to set
	 */
	public void setObsTextura(String obsTextura) {
		this.obsTextura = obsTextura;
	}

	/**
	 * @return the obsSabor
	 */
	public String getObsSabor() {
		return obsSabor;
	}

	/**
	 * @param obsSabor
	 *            the obsSabor to set
	 */
	public void setObsSabor(String obsSabor) {
		this.obsSabor = obsSabor;
	}

	/**
	 * @return the obsOlor
	 */
	public String getObsOlor() {
		return obsOlor;
	}

	/**
	 * @param obsOlor
	 *            the obsOlor to set
	 */
	public void setObsOlor(String obsOlor) {
		this.obsOlor = obsOlor;
	}

	/**
	 * @return the obsColor
	 */
	public String getObsColor() {
		return obsColor;
	}

	/**
	 * @param obsColor
	 *            the obsColor to set
	 */
	public void setObsColor(String obsColor) {
		this.obsColor = obsColor;
	}

	/**
	 * @return the textura
	 */
	public boolean isTextura() {
		return textura;
	}

	/**
	 * @param textura
	 *            the textura to set
	 */
	public void setTextura(boolean textura) {
		this.textura = textura;
	}

	/**
	 * @return the sabor
	 */
	public boolean isSabor() {
		return sabor;
	}

	/**
	 * @param sabor
	 *            the sabor to set
	 */
	public void setSabor(boolean sabor) {
		this.sabor = sabor;
	}

	/**
	 * @return the olor
	 */
	public boolean isOlor() {
		return olor;
	}

	/**
	 * @param olor
	 *            the olor to set
	 */
	public void setOlor(boolean olor) {
		this.olor = olor;
	}

	/**
	 * @return the color
	 */
	public boolean isColor() {
		return color;
	}

	/**
	 * @param color
	 *            the color to set
	 */
	public void setColor(boolean color) {
		this.color = color;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_cheq_fq__traza);

		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

		etMedPhTraza = (EditText) findViewById(R.id.etMedPhTraza);
		etConsisTraza = (EditText) findViewById(R.id.etConsisTraza);
		etAcidezTraza = (EditText) findViewById(R.id.etAcidezTraza);
		etBrixtraza = (EditText) findViewById(R.id.etBrixtraza);
		etObsPre1Traza = (EditText) findViewById(R.id.etObsPre1Traza);
		etObsPre2Traza = (EditText) findViewById(R.id.etObsPre2Traza);
		etObsPre3Traza = (EditText) findViewById(R.id.etObsPre3Traza);
		etObsPre4Traza = (EditText) findViewById(R.id.etObsPre4Traza);
		rbSiResPre1Traza = (RadioButton) findViewById(R.id.rbSiResPre1Traza);
		rbNoResPre1Traza = (RadioButton) findViewById(R.id.rbNoResPre1Traza);
		rbSiResPre2Traza = (RadioButton) findViewById(R.id.rbSiResPre2Traza);
		rbNoResPre2Traza = (RadioButton) findViewById(R.id.rbNoResPre2Traza);
		rbSiResPre3Traza = (RadioButton) findViewById(R.id.rbSiResPre3Traza);
		rbNoResPre3Traza = (RadioButton) findViewById(R.id.rbNoResPre3Traza);
		rbSiResPre4Traza = (RadioButton) findViewById(R.id.rbSiResPre4Traza);
		rbNoResPre4Traza = (RadioButton) findViewById(R.id.rbNoResPre4Traza);
		manejadorMensajes = new handlerAlertMessage();
		manejador = new ManejadorCampos();
		activity = this;

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.layout_cheq_fq__traza, menu);
		return true;
	}

	@SuppressWarnings("deprecation")
	public void tiemMezcla(View v) {
		showDialog(TIME_DIALOG_ID);

	}

	@SuppressLint("SimpleDateFormat")
	private TimePickerDialog.OnTimeSetListener showtime = new TimePickerDialog.OnTimeSetListener() {

		@SuppressLint("SimpleDateFormat")
		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			hHora = hourOfDay;
			hMinuto = minute;

			Context context = getApplicationContext();
			CharSequence text = new StringBuilder().append(hHora).append(":")
					.append(hMinuto).append(" ").append(" ");
			int duration = Toast.LENGTH_SHORT;

			Toast toast = Toast.makeText(context, text, duration);
			toast.show();

			SimpleDateFormat formatoTiempo = new SimpleDateFormat("hh:mm");
			String time = (hHora + ":" + hMinuto).toString();

			try {
				Time tiempo = new Time(formatoTiempo.parse(time).getTime());
				setTiempoMezcla(tiempo);

				CharSequence t = "Hora buena";
				toast = Toast.makeText(context, t, duration);
				toast.show();

			} catch (java.text.ParseException e) {
				CharSequence texto = "Error en la Hora";
				toast = Toast.makeText(context, texto, duration);
				toast.show();

			}

		}
	};

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case TIME_DIALOG_ID:
			return new TimePickerDialog(this, showtime, hHora, hMinuto, true);
		}
		return null;
	}

	// metodo de radiobutton pre1
	public void ifResPre1Traza(View v) {
		// Is the button now checked?
		boolean checked = ((RadioButton) v).isChecked();

		// Check which radio button was clicked

		switch (v.getId()) {
		case R.id.rbSiResPre1Traza:
			if (checked) {

				this.setColor(checked);

				Context context = getApplicationContext();
				CharSequence text = String.valueOf(this.isColor());
				int duration = Toast.LENGTH_SHORT;
				Toast toast = Toast.makeText(context, text, duration);
				toast.show();

			}
			break;
		case R.id.rbNoResPre1Traza:
			if (checked) {
				this.setColor(!checked);

				Context context = getApplicationContext();
				CharSequence text = String.valueOf(this.isColor());
				int duration = Toast.LENGTH_SHORT;
				Toast toast = Toast.makeText(context, text, duration);
				toast.show();

			}
			break;

		}
	}

	// metodo de radiobutton pre2
	public void ifResPre2Traza(View v) {
		// Is the button now checked?
		boolean checked = ((RadioButton) v).isChecked();

		// Check which radio button was clicked

		switch (v.getId()) {
		case R.id.rbSiResPre2Traza:
			if (checked) {

				this.setOlor(checked);

				Context context = getApplicationContext();
				CharSequence text = String.valueOf(this.isOlor());
				int duration = Toast.LENGTH_SHORT;
				Toast toast = Toast.makeText(context, text, duration);
				toast.show();

			}
			break;
		case R.id.rbNoResPre2Traza:
			if (checked) {
				this.setOlor(!checked);
				Context context = getApplicationContext();
				CharSequence text = String.valueOf(this.isOlor());
				int duration = Toast.LENGTH_SHORT;
				Toast toast = Toast.makeText(context, text, duration);
				toast.show();

			}
			break;

		}
	}

	// metodo de radiobutton pre3
	public void ifResPre3Traza(View v) {
		// Is the button now checked?
		boolean checked = ((RadioButton) v).isChecked();

		// Check which radio button was clicked

		switch (v.getId()) {
		case R.id.rbSiResPre3Traza:
			if (checked) {

				this.setSabor(checked);

				Context context = getApplicationContext();
				CharSequence text = String.valueOf(this.isSabor());
				int duration = Toast.LENGTH_SHORT;
				Toast toast = Toast.makeText(context, text, duration);
				toast.show();

			}
			break;
		case R.id.rbNoResPre2Traza:
			if (checked) {
				this.setSabor(!checked);
				Context context = getApplicationContext();
				CharSequence text = String.valueOf(this.isSabor());
				int duration = Toast.LENGTH_SHORT;
				Toast toast = Toast.makeText(context, text, duration);
				toast.show();

			}
			break;

		}
	}

	// metodo de radiobutton pre4
	public void ifResPre4Traza(View v) {
		// Is the button now checked?
		boolean checked = ((RadioButton) v).isChecked();

		// Check which radio button was clicked

		switch (v.getId()) {
		case R.id.rbSiResPre4Traza:
			if (checked) {

				this.setTextura(checked);

				Context context = getApplicationContext();
				CharSequence text = String.valueOf(this.isTextura());
				int duration = Toast.LENGTH_SHORT;
				Toast toast = Toast.makeText(context, text, duration);
				toast.show();

			}
			break;
		case R.id.rbNoResPre4Traza:
			if (checked) {
				this.setTextura(!checked);
				Context context = getApplicationContext();
				CharSequence text = String.valueOf(this.isTextura());
				int duration = Toast.LENGTH_SHORT;
				Toast toast = Toast.makeText(context, text, duration);
				toast.show();

			}
			break;

		}
	}

	// valida que los campos no esten vacios
	public void finalizarChequeoFQ(View v) {
		int medPh = manejador.validarPeso(etMedPhTraza);
		int consist = manejador.numCanastillas(etConsisTraza);
		int acidez = manejador.valoresEnteros(etAcidezTraza);
		int brix = manejador.validarPh(etBrixtraza);

		if (medPh != 0) {
			switch (medPh) {
			case 2:
				manejadorMensajes.alertMessageOneButton(activity,
						R.string.tituloAlerta, R.string.msjMedicPhVac,
						R.string.btnAceptar);
				break;
			}
		} else {
			if (consist != 0) {
				switch (consist) {
				case 5:
					manejadorMensajes.alertMessageOneButton(activity,
							R.string.tituloAlerta, R.string.msjConsVacio,
							R.string.btnAceptar);
					break;
				}
			} else {
				if (acidez != 0) {
					switch (acidez) {
					case 13:
						manejadorMensajes.alertMessageOneButton(activity,
								R.string.tituloAlerta, R.string.msjAcidVacio,
								R.string.btnAceptar);
						break;
					}
				} else {
					if (brix != 0) {
						switch (brix) {
						case 17:
							manejadorMensajes.alertMessageOneButton(activity,
									R.string.tituloAlerta,
									R.string.msjBrixVacio, R.string.btnAceptar);
							break;
						}
					} else {

						this.validar();

					}
				}
			}
		}

	}

	public void validar() {
		if (this.rbNoResPre1Traza.isChecked()
				|| this.rbSiResPre1Traza.isChecked()) {
			if (this.rbSiResPre2Traza.isChecked()
					|| this.rbNoResPre2Traza.isChecked()) {
				if (this.rbNoResPre3Traza.isChecked()
						|| this.rbSiResPre3Traza.isChecked()) {
					if (this.rbSiResPre4Traza.isChecked()
							|| this.rbNoResPre4Traza.isChecked()) {
						if (this.getTiempoMezcla() != null) {

							if (!this.etObsPre1Traza.getText().toString()
									.equals(STRING_EMPTY)) {
								this.setObsColor(this.etObsPre1Traza.getText()
										.toString());
							}

							if (!this.etObsPre2Traza.getText().toString()
									.equals(STRING_EMPTY)) {
								this.setObsOlor(this.etObsPre2Traza.getText()
										.toString());
							}

							if (!this.etObsPre3Traza.getText().toString()
									.equals(STRING_EMPTY)) {
								this.setObsSabor(this.etObsPre3Traza.getText()
										.toString());
							}

							if (!this.etObsPre4Traza.getText().toString()
									.equals(STRING_EMPTY)) {
								this.setObsTextura(this.etObsPre4Traza
										.getText().toString());
							}

							this.setMedPh(this.etMedPhTraza.getText()
									.toString());
							this.setConsistencia(this.etConsisTraza.getText()
									.toString());
							this.setAcidez(this.etAcidezTraza.getText()
									.toString());
							this.setBrix(this.etBrixtraza.getText().toString());

							Context context = getApplicationContext();
							CharSequence text = "¡Muy bien!";
							int duration = Toast.LENGTH_SHORT;

							Toast toast = Toast.makeText(context, text,
									duration);
							toast.show();

							// lo lleva a la vista de trazabilidad
							Intent intent = new Intent();
							intent.setClass(this, Trazabilidad.class);
							startActivity(intent);
						} else {
							// seleccione el tiempo de Mezclado
							manejadorMensajes.alertMessageOneButton(activity,
									R.string.tituloAlerta,
									R.string.msjTiempoMezcla,
									R.string.btnAceptar);
						}
					} else {
						// seleccione el textura
						manejadorMensajes.alertMessageOneButton(activity,
								R.string.tituloAlerta, R.string.msjTextura,
								R.string.btnAceptar);
					}

				} else {
					// selecione el sabor
					manejadorMensajes.alertMessageOneButton(activity,
							R.string.tituloAlerta, R.string.msjSabor,
							R.string.btnAceptar);
				}
			} else {
				// seleccione el olor
				manejadorMensajes.alertMessageOneButton(activity,
						R.string.tituloAlerta, R.string.msjOlor,
						R.string.btnAceptar);
			}

		} else {
			// selecione el color
			manejadorMensajes.alertMessageOneButton(activity,
					R.string.tituloAlerta, R.string.msjColor,
					R.string.btnAceptar);
		}

	}
}
